﻿/*
NTerm: A .NET based terminal abstraction library
Copyright (C) 2011  Bryan Perris

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NTerm
{
    /// <summary>
    /// Represents a terminal host and handles requests sent to it.
    /// </summary>
    public abstract class HostHandler : MarshalByRefObject
    {
        /// <summary>
        /// Initializes the handler and the host terminal
        /// </summary>
        public abstract void Initialize();

        /// <summary>
        /// Dispose the handler and any resources used by the host.
        /// </summary>
        public abstract void Dispose();

        /// <summary>
        /// Requests the host terminal to change its screen cursor position.
        /// </summary>
        /// <param name="row">the row position of the cursor</param>
        /// <param name="col">the column position of the cursor</param>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the host does not allow the specified cursor position</exception>
        public abstract void UpdateCursor(int row, int col);

        /// <summary>
        /// Requests for the number of available columns of the hosts cell buffer.
        /// </summary>
        /// <returns>the width of the buffer in the number of columns</returns>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract int GetBufferWidth();

        /// <summary>
        /// Requests for the number of available rows of the hosts cell buffer.
        /// </summary>
        /// <returns>The height of the buffer in the number of rows</returns>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract int GetBufferHeight();

        /// <summary>
        /// Writes an array of cells to the host buffer
        /// </summary>
        /// <param name="cells">An array of cells to write</param>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        /// <exception cref="System.ArgumentNullException">Thrown when arugment cells is null</exception>
        public abstract void AppendOutput(CharacterCell[] cells);

        /// <summary>
        /// Requests the host to clear screen and write all the cells from the 2D array source
        /// </summary>
        /// <param name="cells">A 2D array of the cells</param>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the array dimensions are bigger than the host buffer size</exception>
        /// <exception cref="System.ArgumentNullException">Thrown when arugment cells is null</exception>
        public abstract void RefreshOutput(CharacterCell[,] cells);

        /// <summary>
        /// Requests the next line from the input stream
        /// </summary>
        /// <returns>A string line</returns>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract string ReadLine();

        /// <summary>
        /// Requests for a user pressed key
        /// </summary>
        /// <param name="intercept">Weather to display the pressed key character</param>
        /// <returns>Key info about the pressed key</returns>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract ConsoleKeyInfo ReadKey(bool intercept);

        /// <summary>
        /// Requests for reading the next character from the input stream
        /// </summary>
        /// <returns>The char value, else -1 for error</returns>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract int Read();
		
        /// <summary>
        /// Requests changing the host buffer size
        /// </summary>
        /// <param name="width">The width of the buffer</param>
        /// <param name="height">The height of the buffer</param>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
		public abstract void SetBufferSize(int width, int height);

        /// <summary>
        /// Rquests the host buffer to clear
        /// </summary>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract void ClearScreen();

        /// <summary>
        /// Requests the host console colors to be changed to the specified ConsoleColors
        /// </summary>
        /// <param name="backgroundColor">The default cell background color</param>
        /// <param name="foregroundColor">The default cell foreground color</param>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract void ResetColor(ConsoleColor backgroundColor, ConsoleColor foregroundColor);

        /// <summary>
        /// Current In textreader used by the host
        /// </summary>
        /// <exception cref="System.NotSupportedException">Thrown when the host does not support this operation</exception>
        public abstract TextReader In
        {
            get;
            set;
        }
    }
}
